package com.cskaoyan.controller;

import com.cskaoyan.annotation.LogAnnotation;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.UserData;
import com.cskaoyan.bean.bo.UserUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.vo.BoardVo;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.bean.vo.UserDetailVo;
import com.cskaoyan.service.UserService;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.scripting.bsh.BshScriptUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author stone
 * @date 2022/06/02 20:41
 */
@RestController
@RequestMapping("admin")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("user/list")
    //public BaseRespVo list(Integer page,Integer limit,String sort,String order,String username) {
    public BaseRespVo list(BaseParam param, String username,String mobile) {
        UserData userData = userService.query(param, username,mobile);
        return BaseRespVo.ok(userData);
    }

    @RequestMapping("dashboard")
    public BaseRespVo board() {
        BoardVo boardVo = userService.queryCountForBoard();
        return BaseRespVo.ok(boardVo);
    }

    @RequestMapping("user/detail")
    public BaseRespVo userDetailByid(Integer id) {
        UserDetailVo userDetailVo = userService.queryUserDetailById(id);
        if (userDetailVo.getId() != -1) {
            return BaseRespVo.ok(userDetailVo);
        } else {
            return BaseRespVo.ok(null);
        }
    }
    @LogAnnotation
    @RequestMapping("auth/logout")
    public BaseRespVo userLogout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok(null);
    }

    @RequestMapping("address/list")
    public BaseRespVo addressList(BaseParam param, Integer userId, String name) {
        TVO tvo = userService.queryAddress(param, userId, name);
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("collect/list")
    public BaseRespVo collectList(BaseParam param, Integer userId, Integer valueId) {
        TVO tvo = userService.queryCollect(param, userId, valueId);
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("footprint/list")
    public BaseRespVo footprintList(BaseParam param, Integer userId, Integer goodsId){
        TVO tvo = userService.queryFootprint(param, userId, goodsId);
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("history/list")
    public BaseRespVo historyList(BaseParam param, Integer userId, String keyword) {
        TVO tvo = userService.queryHistory(param, userId, keyword);
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("feedback/list")
    public BaseRespVo feedbackList(BaseParam param, Integer id, String username) {
        TVO tvo = userService.queryFeedback(param, username, id);
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("region/list")
    public BaseRespVo regionList() {
        TVO tvo = userService.queryRegion();
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("user/update")
    public BaseRespVo updateUserInfo(@RequestBody UserUpdateBo userUpdateBo, HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        userUpdateBo.setLastLoginIp(ip);
        if (!StringUtils.isNumeric(userUpdateBo.getMobile()) || userUpdateBo.getMobile().length() != 11) {
            return BaseRespVo.err("手机号码格式不正确!", 400);
        } else {
            userService.updateUserInfo(userUpdateBo);
            return BaseRespVo.ok(1);
        }
    }


}

